﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace ProyectoFinal
{
    class Datos
    {
        private string rutaIsotoposSQL = "Data Source=localhost;Initial Catalog=Isotopos;Integrated Security=True";

        public SqlConnection cn;
        private SqlCommandBuilder cmb;
        public DataSet ds = new DataSet();
        public SqlDataAdapter da;

        private void Conectar()
        {
            cn = new SqlConnection(rutaIsotoposSQL);
        }

        public Datos()
        {
            Conectar();
        }

        public void Consultar(string sql, string Tabla)
        {
            ds.Tables.Clear();
            da = new SqlDataAdapter(sql, cn);
            cmb = new SqlCommandBuilder(da);
            da.Fill(ds, Tabla);
        }

        public bool Eliminar(string Tabla, string Condicion)
        {
            cn.Open();
            string sql = "delete from " + Tabla + " where " + Condicion;
            SqlCommand Comando = new SqlCommand(sql, cn);
            int i = Comando.ExecuteNonQuery();
            cn.Close();

            if (i > 0)
            {
                return true;
            }
            else {
                return false;
            }
        }

        public bool Actualizar(string Tabla, string campos, string condicion)
        {
            cn.Open();
            string sql = "update " + Tabla + " set " + campos + " where " + condicion;
            SqlCommand Comando = new SqlCommand(sql, cn);
            int i = Comando.ExecuteNonQuery();
            cn.Close();

            if (i > 0)
            {
                return true;
            }
            else {
                return false;
            }
        }

        public DataTable Consultar2(string Tabla)
        {
            string sql = "select * from " + Tabla;
            da = new SqlDataAdapter(sql, cn);
            DataSet dts = new DataSet();
            da.Fill(dts, Tabla);
            DataTable dt = new DataTable();
            dt = dts.Tables[Tabla];

            return dt;
        }

        public bool Insertar(string sql)
        {
            cn.Open();
            SqlCommand Comando = new SqlCommand(sql, cn);
            int i = Comando.ExecuteNonQuery();
            cn.Close();

            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
